package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Goods;
import com.example.springboot.service.IGoodsService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private IGoodsService goodsService;

    @GetMapping
    public Result findGoodsAll() {
        return Result.success(goodsService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(goodsService.getById(id));
    }

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Goods goods) {
        System.out.println("---------------------------------"+goods);
        return Result.success(goodsService.saveOrUpdate(goods));
    }

    // 删除
    @DeleteMapping("/{goodsId}")
    public Result delete(@PathVariable Integer goodsId) {
        return Result.success(goodsService.removeById(goodsId));
    }

    @GetMapping("/page")
    public Result findGoodsPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String goodsName,
                                @RequestParam(defaultValue = "") String goodsType) {
//        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
//
//        if (!"".equals(goodsName)) {
//            queryWrapper.like("goodsName", goodsName);
//        }
//        if (!"".equals(goodsType)) {
//            queryWrapper.like("goodsType", goodsType);
//        }
//        if (!"".equals(address)) {
//            queryWrapper.like("address", address);
//        }
        //获取当前用户信息
//        User currentUser= TokenUtils.getCurrentUser();
//        queryWrapper.orderByDesc("id");
        return Result.success( goodsService.findPage(new Page<>(pageNum, pageSize),goodsName,goodsType));
    }
}
